<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Admin\Controller;
use Admin\Controller\BaseController;
use Think\Model;
/**
 * Description of InfoController
 *
 * @author Administrator
 */
class InfoController extends BaseController {
    function infolist(){
    	$INFO = new Model('Info');
        $CHG = new Model('Charger');
        $AREA = new Model('Area');
    	$totalRows = $INFO->count();
    	$page = new \Think\Page($totalRows, 10);
    	$infolist = $INFO->limit($page->firstRow,$page->listRows)->order('id desc')->select();
        for($i = 0;$i < count($infolist);$i++){
            $qy = $AREA->where(['id'=>$infolist[$i]['area_id']])->find();
            $infolist[$i]['area_name'] = $qy['name'];
            $tmp = $CHG->where(['id'=>$infolist[$i]['charger_id']])->find();
            $infolist[$i]['charger_name'] = $tmp['name'];
            $infolist[$i]['charger_tel'] = $tmp['mobile'];
        }
        $show = $page->show();
        $this->assign('page',$show);
        $this->assign('infolist',$infolist);
    	$this->display('info-infolist');
    }

    function insert(){
    	if(IS_POST){
    		$INFO = D('Info');
            if(!$INFO->create()){
                $this->error($INFO->getError());
            } else {
                $rs = $INFO->add();
                if($rs !== FALSE){
                    $this->success('增加成功');
                } else {
                    $this->error('增加失败');
                }
            }
    	}else{
            $CHG = new Model('Charger');
            $AREA = new Model('Area');
            $chglist = $CHG->order('id desc')->select();
            $arealist = $AREA->order('id desc')->select();
            $this->assign('chglist',$chglist);
            $this->assign('arealist',$arealist);
    		$this->display('info-insert');
    	}
    }

    function delete(){
    	$id = I('id','');
        if($id == ''){
            $this->error('参数错误');
        } else {
            #2，删除操作
            $INFo = new Model('Info');
            $rs = $INFo->where(['id'=>$id])->delete();
            if($rs !== FALSE){
                $this->success('删除成功');
            } else {
                $this->error($INFo->getDbError());
            }
        }
    }

    function edit(){
    	if(IS_POST){
			$id = I('id','');
            if($id != ''){
                $map['id'] = $id;
                if(I('account') != ''){
                    $map['account'] = I('account');
                }
                if(I('name') != ''){
                    $map['name'] = I('name');
                }
                if(I('psn') != ''){
                    $map['psn'] = trim(I('psn'));
                }
                if(I('password') != ''){
                    $map['password'] = I('password');
                }
                if(I('area_id') != ''){
                    $map['area_id'] = I('area_id');
                }
                if(I('data') != ''){
                    $map['data'] = I('data');
                }
                if(I('port') != ''){
                    $map['port'] = I('port');
                }
                if(I('charger_id') != ''){
                    $map['charger_id'] = I('charger_id');
                }
                $INFO = D('Info');
                if(!$INFO->create($map)){
                    $this->error($INFO->getError());
                } else {
                    $rs = $INFO->save();
                    if($rs !== FALSE){
                        $this->success('编辑成功');
                    } else {
                        $this->error('编辑失败');
                    }
                }
            } else {
                $this->error('参数错误');
            }
    	}else{
    		$id = I('id','');
            if($id != ''){
                $INFO = new Model('Info');
                $edit = $INFO->where(['id'=>$id])->find();
                if($edit){
                    $CHG = new Model('Charger');
                    $AREA = new Model('Area');
                    $chglist = $CHG->order('id desc')->select();
                    $arealist = $AREA->order('id desc')->select();
                    $this->assign('chglist',$chglist);
                    $this->assign('arealist',$arealist);
                    $this->assign('edit',$edit);
                    $this->display('info-edit');
                } else {
                    $this->error('信息不存在');
                }
            }else{
                $this->error('参数错误');
            }
    	}
    }

}
